package com.zhangjh.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.zookeeper.KeeperException.Code;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;
import com.zhangjh.dto.User;
import com.zhangjh.dto.UserQueryCondition;
import com.zhangjh.exception.UserNotExistException;

@RestController
@RequestMapping("/user")
public class UserController {
	
	
	@GetMapping
	@JsonView(User.UserSimpleView.class)
	public List<User> queryUserList(UserQueryCondition condition,@PageableDefault(page=2,size=10,sort="userName,asc") Pageable pageable){
		
		System.out.println(ReflectionToStringBuilder.toString(condition,ToStringStyle.MULTI_LINE_STYLE));
		
		System.out.println(pageable.getPageSize());
		System.out.println(pageable.getPageNumber());
		System.out.println(pageable.getSort());
		
		List<User> list = new ArrayList<>();
		list.add(new User());
		list.add(new User());
		list.add(new User());
		return list;
	}
	
	@GetMapping("/{id:\\d+}")
	@JsonView(User.UserDetailView.class)
	public User getInfo(@PathVariable String id){
//		throw new UserNotExistException(id);
		System.out.println("进入getInfo服务！");
		User user = new User();
		user.setUserName("tom");
		return user;
	}
	
	@PostMapping
	@JsonView(User.UserDetailView.class)
	public User create(@Valid @RequestBody User user,BindingResult errors){
		
		if(errors.hasErrors()){
			errors.getAllErrors().stream().forEach(error -> System.out.println(error.getDefaultMessage()));
		}
		System.out.println("bofore:"+user);
		user.setId(1);
		System.out.println("after:"+user);
		return user;
	
	}
	
	@PutMapping("/{id:\\d+}")
	@JsonView(User.UserDetailView.class)
	public User update(@Valid @RequestBody User user,BindingResult errors){
		
		if(errors.hasErrors()){
			errors.getAllErrors().stream().forEach(error -> {
				FieldError fieldError = (FieldError) error;
				String message = fieldError.getField() +":"+fieldError.getDefaultMessage();
				System.out.println(message);
			});
		}
		System.out.println("bofore:"+user);
		user.setId(1);
		System.out.println("after:"+user);
		return user;
	
	}
	
	@DeleteMapping("/{id:\\d+}")
	@JsonView(User.UserDetailView.class)
	public void delete(@PathVariable Integer id){
		System.out.println(id);
	
	}
}
